package bluetooth.main;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import bluetooth.data.DeserializeData;
import bluetooth.data.SendNotificationToAllDevices;
import bluetooth.model.Product;

import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Group;

public class frmShowRecivedData extends Shell {
	private List list;
	private Button btnSendToAllAndroidDevices;
	private Button btnUpdate;
	private Text txtSelectedItem;
	private Button btnRemove;
	private Group grpUpdate;
	private Group grpRemove;
	private Group grpAddNewItem;
	private Text txtNewItem;
	private Button btnAdd;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			frmShowRecivedData shell = new frmShowRecivedData(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * @param display
	 */
	public frmShowRecivedData(Display display) {
		super(display, SWT.SHELL_TRIM);
		setLayout(new FormLayout());
		
		list = new List(this, SWT.BORDER);
		list.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				
				//item selected
				txtSelectedItem.setText(DeserializeData.getProducts().get(list.getSelectionIndex()).getName());
				
			}
		});
		FormData fd_list = new FormData();
		fd_list.left = new FormAttachment(0, 10);
		fd_list.top = new FormAttachment(0, 10);
		fd_list.right = new FormAttachment(100, -182);
		
		list.setLayoutData(fd_list);
		
		btnSendToAllAndroidDevices = new Button(this, SWT.NONE);
		btnSendToAllAndroidDevices.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				
				//Call airPUSH send api 
				 SendNotificationToAllDevices.sendListToAllDevices();
				
			}
		});
		FormData fd_btnSendToAllAndroidDevices = new FormData();
		fd_btnSendToAllAndroidDevices.top = new FormAttachment(list, 6);
		fd_btnSendToAllAndroidDevices.right = new FormAttachment(list, 0, SWT.RIGHT);
		fd_btnSendToAllAndroidDevices.bottom = new FormAttachment(100, -10);
		btnSendToAllAndroidDevices.setLayoutData(fd_btnSendToAllAndroidDevices);
		btnSendToAllAndroidDevices.setText("Send to all Android devices");
		
		grpUpdate = new Group(this, SWT.NONE);
		grpUpdate.setText("Update");
		FormData fd_grpUpdate = new FormData();
		fd_grpUpdate.left = new FormAttachment(list, 12);
		fd_grpUpdate.right = new FormAttachment(100, -10);
		fd_grpUpdate.bottom = new FormAttachment(0, 101);
		fd_grpUpdate.top = new FormAttachment(0, 10);
		grpUpdate.setLayoutData(fd_grpUpdate);
		
		grpRemove = new Group(this, SWT.NONE);
		grpRemove.setText("Remove");
		FormData fd_grpRemove = new FormData();
		fd_grpRemove.left = new FormAttachment(list, 12);
		fd_grpRemove.right = new FormAttachment(100, -10);
		fd_grpRemove.bottom = new FormAttachment(grpUpdate, 97, SWT.BOTTOM);
		fd_grpRemove.top = new FormAttachment(grpUpdate, 6);
		
		txtSelectedItem = new Text(grpUpdate, SWT.BORDER);
		txtSelectedItem.setBounds(10, 10, 136, 19);
		
		btnUpdate = new Button(grpUpdate, SWT.NONE);
		btnUpdate.setBounds(33, 35, 90, 28);
		btnUpdate.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				
             if(txtSelectedItem.getText().trim().length()==0){
					
					MessageBox box = new MessageBox(getShell(), SWT.CANCEL | SWT.OK);
					box.setText("Validation");
					box.setMessage("Enter new value for selected item..");

					box.open();
					
				}else{
				//Update 	
				Product product_update= new Product ();
				product_update.setName(txtSelectedItem.getText());
				DeserializeData.getProducts().set(list.getSelectionIndex(),product_update);
				list.removeAll();
				
				 for (int i=0; i< DeserializeData.getProducts().size(); i++){
					 
					 list.add("Name:"+DeserializeData.getProducts().get(i).getName());// +"   Category:"+ DeserializeData.getProducts().get(i).getCategory().getName());
					 list.setSelection(list.getItemCount()-1);
					 
				 }
				
				}
			
			}
		});
		btnUpdate.setText("Update");
		grpRemove.setLayoutData(fd_grpRemove);
		
		btnRemove = new Button(grpRemove, SWT.NONE);
		btnRemove.setBounds(32, 26, 88, 28);
		btnRemove.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				
				//remove selected item 
				DeserializeData.getProducts().remove(list.getSelectionIndex());
				list.removeAll();
				
				 for (int i=0; i< DeserializeData.getProducts().size(); i++){
					 
					 list.add("Name:"+DeserializeData.getProducts().get(i).getName());// +"   Category:"+ DeserializeData.getProducts().get(i).getCategory().getName());
					 list.setSelection(list.getItemCount()-1);
					 
				 }
				
			}
		});
		btnRemove.setText("Remove");
		
		grpAddNewItem = new Group(this, SWT.NONE);
		fd_list.bottom = new FormAttachment(grpAddNewItem, 0, SWT.BOTTOM);
		grpAddNewItem.setText("Add new item");
		FormData fd_grpAddNewItem = new FormData();
		fd_grpAddNewItem.left = new FormAttachment(list, 12);
		fd_grpAddNewItem.right = new FormAttachment(100, -10);
		fd_grpAddNewItem.bottom = new FormAttachment(grpRemove, 97, SWT.BOTTOM);
		fd_grpAddNewItem.top = new FormAttachment(grpRemove, 6);
		grpAddNewItem.setLayoutData(fd_grpAddNewItem);
		
		txtNewItem = new Text(grpAddNewItem, SWT.BORDER);
		txtNewItem.setBounds(10, 10, 136, 19);
		
		btnAdd = new Button(grpAddNewItem, SWT.NONE);
		btnAdd.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				
				//add new item
				if(txtNewItem.getText().trim().length()==0){
					
					MessageBox box = new MessageBox(getShell(), SWT.CANCEL | SWT.OK);
					box.setText("Validation");
					box.setMessage("Enter value for new item...");

					box.open();
					
				}
				else
				{
				
				Product product= new Product ();
				product.setName(txtNewItem.getText());
				DeserializeData.getProducts().add(product);
				list.removeAll();
				
				 for (int i=0; i< DeserializeData.getProducts().size(); i++){
					 
					 list.add("Name:"+DeserializeData.getProducts().get(i).getName());// +"   Category:"+ DeserializeData.getProducts().get(i).getCategory().getName());
					 list.setSelection(list.getItemCount()-1);
					 
				 }
				 
				}
				
			}
		});
		btnAdd.setBounds(30, 36, 94, 28);
		btnAdd.setText("Add");
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("Data from Android");
		setSize(466, 398);

		//Display recived data from android 
		
		 for (int i=0; i< DeserializeData.getProducts().size(); i++){
			 
			 list.add("Name:"+DeserializeData.getProducts().get(i).getName());// +"   Category:"+ DeserializeData.getProducts().get(i).getCategory().getName());
			 list.setSelection(list.getItemCount()-1);
			 
		 }
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
